<?php
include_once 'BaseS.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/NewsM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Util.php';

class NewsS extends BaseS {
	
	public function __construct() {
		parent::__construct ();
		parent::setTarget ( $this );
	}

	public function insert() {
		
		$result = array("sl"=>"1", "di"=>"0", "ti"=>"新增結果", "msg"=>"新增失敗！"	);
		$obj = $this->getRequest("datas");
		$dao = new NewsM();
		$obj['date_create'] = Util::getNowDate("Y-m-d H:i:s");
		
		if(isset($obj['photo'])){ $obj['photo']= $this->changePhotoPath($obj['photo']); }
		if($dao->insert($obj)){$result['msg'] = "新增成功！";}
		
		$this->logUserAction(Util::clientIp(), "最新消息", 1);
		echo Util::toJson($result);
	}
	
	public function update(){
		
		$result = array("sl"=>"1", "di"=>"1","ti"=>"修改結果", "msg"=>"修改失敗!");
		$obj = $this->getRequest("datas");
		$dao = new NewsM();
		
		if(isset($obj['photo'])){ $obj['photo']= $this->changePhotoPath($obj['photo']); }
		if($dao->update($obj)){$result['msg'] = "修改成功！";}
		
		$this->logUserAction(Util::clientIp(), "最新消息", 2);
		
		if($_SESSION["user"]->id == $obj['id']){
			$_SESSION["user"] = $dao->getUser($_SESSION["user"]->account, $obj['ps_e']);
		}
		
		echo Util::toJson($result);
	}
	
	public function delete(){
		
		$result = array("sl"=>"1", "di"=>"1","ti"=>"刪除結果", "msg"=>"刪除失敗!");
		$id = $this->getRequest("id");
		$dao = new NewsM();
		if($dao->delete($id)){$result['msg'] = "刪除成功！";}
		$this->logUserAction(Util::clientIp(), "最新消息", 3);
		echo Util::toJson($result);
	}
	
	public function isDelete(){
	
		$result = array("sl"=>"1", "di"=>"1","ti"=>"刪除結果", "msg"=>"刪除失敗!");
		$obj = $this->getRequest("datas");
		$dao = new NewsM();
	
		if(isset($obj['password'])){$obj['ps_e'] = Util::encrypt($obj['password']);}
		if($dao->update($obj)){$result['msg'] = "刪除成功！";}
	
		$this->logUserAction(Util::clientIp(), "最新消息", 3);
	
		if($_SESSION["user"]->id == $obj['id']){
			$_SESSION["user"] = $dao->getUser($_SESSION["user"]->account, $obj['ps_e']);
		}
	
		echo Util::toJson($result);
	}
	
	public function getTableData(){
		$result = array("sl"=>"1", "di"=>"0", "ti"=>"",	"msg"=> "");
		$key = $this->getRequest("dataId");
		$dao = new NewsM();
		$result['data'] = $dao->getTableData($key);
		
		if($result["data"] == null){
			$result["di"] = "1";
			$result["ti"] = "查無資料";
			$result["msg"] = "資料庫沒有相關資料！";
		}
		
		echo Util::toJson($result);
	}
	
	public function queryTable(){
		 
		$result = array("sl"=>"1", "di"=>"0", "ti"=>"", "msg"=>"");
		
		$dao = new NewsM();
		$obj = $this->getRequest("datas");
		$startPage = ($obj['page']-1) * $obj['pageRow']; 
		$searchStr = "";
		$result["data"] = $dao->queryTable($obj['orderData'], $startPage, $obj['pageRow'], $searchStr);
		
		foreach($result["data"]["result"] as $row){
			if(mb_strlen($row->title, "utf-8") > 30){$row->title = mb_substr($row->title, 0, 30, "utf-8")."...";}
			if(mb_strlen($row->content, "utf-8") > 30){$row->content = mb_substr($row->content, 0, 30, "utf-8")."...";}
		}
		
		if($result["data"] == null){
			$result["di"] = "1";
			$result["ti"] = "系統異常";
			$result["msg"] = "資料庫無法連接！";
		}
		
		if($result["data"]['count'] == 0){
			$result["di"] = "1";
			$result["ti"] = "查無資料";
			$result["msg"] = "資料庫沒有相關資料！";
		}
		
		echo Util::toJson($result); 
	}
	
}

if (isset($_GET ["action"])) {
	$service = new NewsS ();
	$service->__call ( $_GET ["action"], null );
} 
?>